package com.toe.love.webapi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.toe.common.exception.ToeServiceException;
import com.toe.common.restful.JsonPage;
import com.toe.common.restful.StateEnum;
import com.toe.love.service.LoveGoodsService;
import com.toe.love.webapi.mapper.LoveGoodsMapper;
import com.toe.pojo.dto.love.LoveGoodInfoDTO;
import com.toe.pojo.vo.love.LoveGoodInfoVO;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@DubboService
public class LoveGoodsServiceImpl implements LoveGoodsService {
    @Autowired
    private LoveGoodsMapper loveGoodsMapper;

    @Override
    public void addLoveGood(LoveGoodInfoDTO loveGoodInfoDTO) {
        int rows = loveGoodsMapper.insertLoveGood(loveGoodInfoDTO);
        if(rows != 1) {
            throw new ToeServiceException(StateEnum.ERR_SERVER_BUSY,"服务器繁忙，上架爱心商品失败");
        }
    }

    @Override
    public JsonPage<LoveGoodInfoVO> pageLoveGoods(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<LoveGoodInfoVO> list = loveGoodsMapper.selectLoveGoods();
        return JsonPage.restPage(new PageInfo<>(list));
    }

    @Override
    public void removeLoveGood(Long id) {
        int rows = loveGoodsMapper.deleteLoveGood(id);
        System.out.println(">>>>>>>>>>>>rows: " + rows);
        if(rows != 1) {
            throw new ToeServiceException(StateEnum.ERR_SERVER_BUSY,"服务器繁忙，下架爱心商品失败");
        }
    }
}
